package com.msh.artascope.devcloud.service.api;

import com.msh.artascope.devcloud.client.enums.biz.JenkinsBuildResultEnum;
import com.msh.artascope.devcloud.client.po.ModuleBuildPo;
import com.msh.artascope.devcloud.client.qo.ModuleBuildQo;
import com.msh.artascope.devcloud.service.model.query.ModuleBuildQueryVo;
import com.msh.artascope.devcloud.service.model.vo.BuildLogVo;
import com.msh.artascope.devcloud.service.model.vo.ModuleBuildVo;
import com.msh.artascope.devcloud.service.service.ModuleBuildService;
import com.msh.artascope.interfaces.IAccountGet;
import com.msh.artascope.interfaces.ILoginAccount;
import com.msh.frame.client.base.SortBy;
import com.msh.frame.client.common.Result;
import com.msh.frame.client.define.ColumnDef;
import com.msh.frame.client.enums.YesNoEnum;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.client.validation.IInsertVo;
import com.msh.frame.client.validation.IListVo;
import com.msh.frame.web.base.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Tag(name = "构建记录-api相关接口")
@Slf4j
@RestController
@RequestMapping("/api/moduleBuild")
public class ModuleBuildApi extends BaseController<ModuleBuildPo, ModuleBuildQo> {

  @Autowired
  private ModuleBuildService moduleBuildService;

  @Autowired
	private IAccountGet accountGet;


  @Operation(summary = "构建模块")
  @PostMapping("build")
  public Result<ModuleBuildVo> build(@Validated({IInsertVo.class,Default.class}) @RequestBody ModuleBuildVo p) {
    p.setId(null);
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    p.initLoginAccount(loginAccount);
    return Result.successReturn(moduleBuildService.build(p));
  }

  @Operation(summary = "获取详情")
  @GetMapping("log")
  public Result<BuildLogVo> log(@NotNull(message = "buildId") @RequestParam("buildId") Long buildId, @RequestParam(value = "bufferOffset", required = false) Integer bufferOffset) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    if(null == bufferOffset){
      bufferOffset = 0;
    }
    return Result.successReturn(moduleBuildService.log(buildId, bufferOffset, loginAccount.getTenantId()));
  }

  @Operation(summary = "获取详情")
  @GetMapping("get")
  public Result<ModuleBuildVo> get(@NotNull(message = "id不能为空") @RequestParam("id") Long id) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    return Result.successReturn(moduleBuildService.getVo(id, loginAccount.getTenantId()));
  }

  @Operation(summary = "vo列表查询")
  @PostMapping("list")
  public Result<List<ModuleBuildVo>> list(@Validated({IListVo.class}) @RequestBody ModuleBuildQueryVo q) {
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return Result.successReturn(moduleBuildService.listVo(q));
  }


  @Operation(summary = "vo列表分页查询")
  @PostMapping("page")
  public Result<List<ModuleBuildVo>> page(
      @Validated({IListVo.class}) @RequestBody ModuleBuildQueryVo q) {
    if (null == q.getSortBy()) {
      q.setSortBy(SortBy.desc(ColumnDef.ID));
    }
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    return moduleBuildService.pageVo(q);
  }


  @Operation(summary = "查询可以运行的构建记录")
  @PostMapping("listRunEnable")
  public Result<List<ModuleBuildVo>> listRunEnable(
      @Validated({IListVo.class}) @RequestBody ModuleBuildQueryVo q) {
    q.setSortBy(SortBy.desc(ColumnDef.ID));
    q.setRunningFlag(YesNoEnum.NO.getValue());
    q.setPageSize(10);
    q.setResult(JenkinsBuildResultEnum.SUCCESS.getDesc());
    ILoginAccount loginAccount = accountGet.getLoginAccount();
    BizException.doThrowIfNull("无法获取登录账号信息", loginAccount);
    q.initLoginAccount(loginAccount);
    List<ModuleBuildVo> buildVoList = moduleBuildService.listVo(q);
    buildVoList.forEach(item -> item.setImageName(
        item.getImageName().substring(item.getImageName().lastIndexOf("/") + 1)));
    return Result.successReturn(buildVoList);
  }
}
